/*
 * SQLIndiceBursatilDAOFactory.java
 *
 * Created on 31 de octubre de 2007, 16:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package webportlet.model.indiceBursatil.dao;

/**/
import java.sql.Connection;
import java.util.ArrayList;
import javax.sql.DataSource;

import webportlet.model.indiceBursatil.dto.*;
import webportlet.model.indiceBursatil.dao.*;
import webportlet.model.util.*;

/**
 *
 * @author Administrador
 */
public class SQLIndiceBursatilDAOFactory {
    
     private final static String DAO_CLASS_NAME_PARAMETER =
        "SQLIndiceBursatilDAOFactory/daoClassName";
     
     private final static Class daoClass = getDAOClass();
   
   private SQLIndiceBursatilDAOFactory(){}
   
   private static Class getDAOClass()
   {
       Class theClass = null;
       try
       {
            // Coge los parametros del archivo ConfigurationParameters
            /*
            String daoClassName = //"model.user.StandarSQLUserDAO";
                ConfigurationParametersManager.getParameter(
                    DAO_CLASS_NAME_PARAMETER);
            
            theClass = Class.forName(daoClassName);*/
           
           
           theClass = Class.forName("webportlet.model.indiceBursatil.dao.CCSQLIndiceBursatilDAO");
       
           
       }catch(Exception e){
            e.printStackTrace();
        }
        return theClass;
   
   }
   public static SQLIndiceBursatilDAO getDAO() throws InternalErrorException {
        
       try {        
            return (SQLIndiceBursatilDAO) daoClass.newInstance();
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }
   
   public static void main (String[] args)
   {
       Connection connection = null;                                             
                                                                                  
        try{                                                                     
                                                                                  
            /* Get a connection. */                                               
            DataSource dataSource = new SimpleDataSource();                       
            connection = dataSource.getConnection();                              
                                                                                                                                                    //
            SQLIndiceBursatilDAO dao = SQLIndiceBursatilDAOFactory.getDAO();            
                                                                                  
            /* Test "SQLUserProfileDAO::create". */                               
            System.out.println("Test for 'SQLIndiceBursatilDAO::create'");           
            IndiceBursatilDTO indiceBursatilDTO = new IndiceBursatilDTO(new Long(001), "ZARA", new Double(15.2));                                
                                        
            indiceBursatilDTO = dao.create(connection, indiceBursatilDTO);                               
            System.out.println("indiceBursatilDTO created => " + indiceBursatilDTO);   
            Long id = indiceBursatilDTO.getId();
                                                                                 
            /* Test "SQLUserProfileDAO::exists". */                              
            System.out.println("Test for 'SQLIndiceBursatilDAO::exists'");           
           if (!dao.exits(connection,indiceBursatilDTO.getId())) {         
                throw new Exception("Can not find user profile with " +           
                    "Id = " +                                              
                    indiceBursatilDTO.getId());                               
            }                                                               
                                                                            
            /* Test "SQLUserProfileDAO::find". */                           
            System.out.println("Test for 'SQLIndiceBursatilDAO::find'");           
            System.out.println(dao.find(connection,indiceBursatilDTO.getId()));         
            
            
            /* Test "SQLUserProfileDAO::update". */                    
            System.out.println("Test for 'SQLUserProfileDAO::update'");                         
            indiceBursatilDTO.setEmpresa("Telefonica");                            
            indiceBursatilDTO.setValor(new Double(1.7));                    
                                         
                               
            dao.update(connection, indiceBursatilDTO);                
            System.out.println(dao.find(connection, indiceBursatilDTO.getId()));                                 
                                                                                  
            /* Test "SQLUserProfileDAO::remove". */                               
            System.out.println("Test for 'SQLUserProfileDAO::remove'");           
            dao.remove(connection, indiceBursatilDTO.getId());                
                                                                                  
           if (dao.exits(connection,indiceBursatilDTO.getId())) {          
                throw new Exception("SQLUserProfileDAO::remove failed");   
                
            }   
            /* Para hacer este test es necesario haber ejecutado los scripts
             * de creacion de base de datos.
             * Test "SQLIndiceBursatilDAO:recuperarlistacompleta". */
            System.out.println("Test for 'SQLIndiceBursatilDAO::recuperarlistacompleta'");
            
            
            ArrayList <IndiceBursatilDTO> listaDTOs = new ArrayList();
            listaDTOs = dao.recuperarListaCompleta(connection);
            for(int i=0; i<listaDTOs.size();i++){
                System.out.println(listaDTOs.get(i));
            }
            
            /* Para hacer este test es necesario haber ejecutado los scripts
             * de creacion de base de datos.
             * Test "SQLIndiceBursatilDAO:recuperarlista". */
            System.out.println("Test for 'SQLIndiceBursatilDAO::recuperarlista'");
            ArrayList <Long> listaIds = new ArrayList();
            listaIds.add(new Long(1));
            listaIds.add(new Long(2));
            listaIds.add(new Long(3));
            listaDTOs.clear();
            listaDTOs = dao.recuperarLista(connection,listaIds);
            for(int i=0; i<listaDTOs.size();i++){
                System.out.println(listaDTOs.get(i));
            }
            /* Tests OK. */                                                       
            System.out.println("Tests OK !!!!");                                  
                                                                                  
        } catch (Exception e) {                                                  
            e.printStackTrace();                                               
        } finally {                                                            
            try {                                                              
                GeneralOperations.closeConnection(connection);                 
            } catch (InternalErrorException e) {                               
                e.printStackTrace();                                           
            }                                                                  
        }                                                                      
                                     
      }
    
}
